package com.elm.view.impl;

import com.elm.dao.BusinessDao;
import com.elm.dao.impl.BusinessDaoImpl;
import com.elm.entity.Business;
import com.elm.view.BusinessView;

import java.util.List;
import java.util.Scanner;

public class BusinessViewImpl implements BusinessView {
    private Scanner input = new Scanner(System.in);

    @Override
    public void listAllBusinesses() {
        BusinessDao dao = new BusinessDaoImpl();
        List<Business> list = dao.listBusiness(null, null);
        System.out.println("商家编号\t商家名称\t商家地址\t商家介绍\t起送费\t配送费");
        for (Business b : list) {
            System.out.println(b.getBusinessId() + "\t" + b.getBusinessName() + "\t" + b.getBusinessAddress() + "\t" + b.getBusinessExplain() + "\t" + b.getStarPrice() + "\t" + b.getDeliveryPrice());
        }
    }

    @Override
    public void listBusinessesBySearch() {
        String businessName = "";
        String businessAddress = "";
        System.out.println("请输入商家名称关键词（可为空）：");
        businessName = input.next();
        System.out.println("请输入商家地址关键词（可为空）：");
        businessAddress = input.next();

        BusinessDao dao = new BusinessDaoImpl();
        List<Business> list = dao.listBusiness(businessName, businessAddress);
        System.out.println("商家编号\t商家名称\t商家地址\t商家介绍\t起送费\t配送费");
        for (Business b : list) {
            System.out.println(b.getBusinessId() + "\t" + b.getBusinessName() + "\t" + b.getBusinessAddress() + "\t" + b.getBusinessExplain() + "\t" + b.getStarPrice() + "\t" + b.getDeliveryPrice());
        }
    }

    @Override
    public void saveBusiness() {
        Business business = new Business();
        System.out.println("请输入商家名称：");
        business.setBusinessName(input.next());
        System.out.println("请输入商家地址：");
        business.setBusinessAddress(input.next());
        System.out.println("请输入商家介绍：");
        business.setBusinessExplain(input.next());
        System.out.println("请输入起送费：");
        business.setStarPrice(input.nextDouble());
        System.out.println("请输入配送费：");
        business.setDeliveryPrice(input.nextDouble());

        BusinessDao dao = new BusinessDaoImpl();
        int result = dao.saveBusiness(business);
        if (result > 0) {
            System.out.println("新增商家成功！");
        } else {
            System.out.println("新增商家失败！");
        }
    }

    @Override
    public void removeBusiness() {
        System.out.println("请输入要删除的商家编号：");
        String businessId = input.next();
        BusinessDao dao = new BusinessDaoImpl();
        int result = dao.removeBusiness(businessId);
        if (result > 0) {
            System.out.println("删除商家成功！");
        } else {
            System.out.println("删除商家失败！");
        }
    }

    @Override
    public Business login() {
        System.out.println("请输入商家编号：");
        String businessId = input.next();
        System.out.println("请输入密码：");
        String password = input.next();

        BusinessDao dao = new BusinessDaoImpl();
        Business business = dao.getBusinessById(businessId);
        if (business != null && business.getPassword().equals(password)) {
            return business;
        } else {
            return null;
        }
    }

    @Override
    public void showBusiness(String businessId) {
        BusinessDao dao = new BusinessDaoImpl();
        Business business = dao.getBusinessById(businessId);
        if (business != null) {
            System.out.println(business);
        } else {
            System.out.println("商家不存在！");
        }
    }

    @Override
    public void editBusiness(String businessId) {
        BusinessDao dao = new BusinessDaoImpl();
        Business business = dao.getBusinessById(businessId);
        if (business == null) {
            System.out.println("商家不存在！");
            return;
        }

        System.out.println("请输入新的商家名称（" + business.getBusinessName() + "）：");
        business.setBusinessName(input.next());
        System.out.println("请输入新的商家地址（" + business.getBusinessAddress() + "）：");
        business.setBusinessAddress(input.next());
        System.out.println("请输入新的商家介绍（" + business.getBusinessExplain() + "）：");
        business.setBusinessExplain(input.next());
        System.out.println("请输入新的起送费（" + business.getStarPrice() + "）：");
        business.setStarPrice(input.nextDouble());
        System.out.println("请输入新的配送费（" + business.getDeliveryPrice() + "）：");
        business.setDeliveryPrice(input.nextDouble());

        int result = dao.updateBusiness(business);
        if (result > 0) {
            System.out.println("商家信息修改成功！");
        } else {
            System.out.println("商家信息修改失败！");
        }
    }

    @Override
    public void updateBusinessByPassword(String businessId) {
        BusinessDao dao = new BusinessDaoImpl();
        Business business = dao.getBusinessById(businessId);
        if (business == null) {
            System.out.println("商家不存在！");
            return;
        }

        System.out.println("请输入旧密码：");
        String oldPassword = input.next();
        if (!business.getPassword().equals(oldPassword)) {
            System.out.println("旧密码不正确！");
            return;
        }

        System.out.println("请输入新密码：");
        String newPassword = input.next();
        System.out.println("请再次输入新密码：");
        String confirmPassword = input.next();

        if (!newPassword.equals(confirmPassword)) {
            System.out.println("两次输入密码不一致！");
            return;
        }

        int result = dao.updateBusinessByPassword(businessId, newPassword);
        if (result > 0) {
            System.out.println("密码修改成功！");
        } else {
            System.out.println("密码修改失败！");
        }
    }
}
